﻿using StudentsInformation.Models;
using System;
using System.Collections.Generic;
using System.IO;

namespace StudentsInformation
{
    /// <summary>
    /// 导入学生信息，并统计学生成绩的平均分，最高分和最低分
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //将文本文件每一行存入数组
            string[] lines = File.ReadAllLines(@"F:\VShomework2021\csharp2021-home-work\StudentsInformation\StudentData.txt");
            //创建学生列表
            List<Student> students = new List<Student>();
            //向列表中添加对象
            for (int i = 1; i < lines.Length; i++)
            {
                var Student = new Student();
                string line = lines[i];
                //按列分隔
                string[] items = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                Student.Name = items[0];
                Student.StudentNumber = items[1];
                Student.PhoneNumber = items[2];
                Student.Birthday = items[3];
                Student.Garde = Convert.ToInt32(items[4]);
                //把建立的学生类中的对象放到队列中
                students.Add(Student);
            }
            //计算平均分,最高分和最低分
            double averageGrade;
            int compare = students[0].Garde;
            double sum = 0;
            var maxStudent = students[0];
            var minStudent = students[0];
            for (int i = 0; i < students.Count; i++)
            {
                if (students[i].Garde > compare)
                {
                    maxStudent = students[i];
                    compare = students[i].Garde;
                }
                if (students[i].Garde < compare)
                {
                    minStudent = students[i];
                    compare = students[i].Garde;
                }
                sum += students[i].Garde;
            }
            averageGrade = sum / students.Count;//计算平均成绩
            averageGrade = Math.Round(averageGrade, 2);//保留两位小数
            //输出平均分，最高分和最低分学生信息
            Console.WriteLine("平均分为：{0}", averageGrade);
            Console.WriteLine("---------------------------------------------------------");
            Console.WriteLine($"最高分为{maxStudent.Garde}，其姓名为{maxStudent.Name}，学号为{maxStudent.StudentNumber}，电话号为{maxStudent.PhoneNumber}");
            Console.WriteLine($"最低分为{minStudent.Garde}，其姓名为{minStudent.Name}，学号为{minStudent.StudentNumber}，电话号为{minStudent.PhoneNumber}");
            Console.ReadKey();
        }
    }
}
